package im.xz.cn.xiacore.tpPlayer;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class tpp implements CommandExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
        try {
            if (commandSender instanceof Player) {
                try {
                    if (strings.length == 1) {
                        String target = strings[0];
                        Player player = (Player) commandSender;
                        String name = player.getName();
                        try {
                            if (target.equalsIgnoreCase(name)) {
                                player.sendMessage(ChatColor.RED + "你不能传送自己");
                                return false;
                            }
                        }catch (NullPointerException ignore) {

                        }
                        for (Object o : player.getServer().getOnlinePlayers().toArray()) {
                            try {
                                Player awa = (Player) o;
                                if ((awa.getName().toString().equalsIgnoreCase(target))) {
                                    Player targetPlayer = (Player) o;
                                    targetPlayer.sendMessage(ChatColor.AQUA + name +  "申请传送至你，操作传送物品以接受，扔掉以忽略/拒绝");
                                    targetPlayer.sendMessage(ChatColor.GRAY + "您的背包已被添加一个传送快捷选择物(背包满将无法得到)，扔掉拒绝传送，右键使用为允许传送");
                                    try {
                                        if (!targetPlayer.getInventory().contains(Material.AIR)) {
                                            try {
                                                for (int i = 0;i<=35;i++) {
                                                    ItemStack item1 = new ItemStack(Material.PAPER);
                                                    ItemMeta meta = item1.getItemMeta();
                                                    meta.setDisplayName(ChatColor.RED + "传送快捷选择物");
                                                    meta.setEnchantmentGlintOverride(true);
                                                    ArrayList<String> lore = new ArrayList<>();
                                                    lore.add(ChatColor.WHITE + "操作使用以接受传送，扔掉拒绝传送");
                                                    lore.add("请求来自 ");
                                                    lore.add(name);
                                                    meta.setLore(lore);
                                                    item1.setItemMeta(meta);
                                                    targetPlayer.getInventory().addItem(item1);
                                                    player.sendMessage(ChatColor.GREEN + "请求已发送(如果玩家不存在或对方背包已满，则无法成功发送请求)");
                                                    break;
                                                }
                                            }catch (NullPointerException ignore) {

                                            }
                                        }else {
                                            targetPlayer.sendMessage(ChatColor.RED + "您的背包已满！传送快捷选择物无法给予你！请清理背包后要求对方重新输入指令！");
                                        }
                                    }catch (NullPointerException ignore) {

                                    }
                                    break;
                                }else {
                                }

                            }catch (NullPointerException ignore) {
                            }
                        }

                    }else {
                        commandSender.sendMessage(ChatColor.RED + "语法错误");
                    }
                }catch (NullPointerException ignore) {

                }
            }else {
                System.out.println("this command can only be ran by player");
            }
        }catch (NullPointerException ignore) {

        }

        return false;
    }
